/**
 * 一些通用方法
 */
(function(exports) {

	/**
	 * 将string字符串转为html对象,默认创一个div填充
	 * 因为很常用，所以单独提取出来了
	 * @param {String} strHtml 目标字符串
	 * @return {HTMLElement} 返回处理好后的html对象,如果字符串非法,返回null
	 */
	exports.parseHtml = function(strHtml) {
		if(typeof strHtml !== 'string') {
			return strHtml;
		}
		// 创一个灵活的div
		var i,
			a = document.createElement('div');
		var b = document.createDocumentFragment();

		a.innerHTML = strHtml;

		while((i = a.firstChild)) {
			b.appendChild(i);
		}

		return b;
	};

	/**
	 * 将对象渲染到模板
	 * @param {String} template 对应的目标
	 * @param {Object} obj 目标对象
	 * @return {String} 渲染后的模板
	 */
	exports.renderTemplate = function(template, obj) {
		return template.replace(/[{]{2}([^}]+)[}]{2}/g, function($0, $1) {
			return obj[$1] || '';
		});
	};

	/**
	 * 定义一个计数器
	 */
	//  var counterArr = [0];

	/**
	 * 添加测试数据
	 * @param {String} dom 目标dom
	 * @param {Number} count 需要添加的数量
	 * @param {Boolean} isReset 是否需要重置，下拉刷新的时候需要
	 * @param {Number} index 属于哪一个刷新
	 */
	var datapp = "";
	exports.appendTestData = function(dom, count, isReset, index, cishu) {
//		console.log(index)
//		console.log(cishu)
		if(typeof dom === 'string') {
			dom = document.querySelector(dom);
		}

		//      var prevTitle = typeof index !== 'undefined' ? ('Tab' + index) : '';

		//      var counterIndex = index || 0;

		//      counterArr[counterIndex] = counterArr[counterIndex] || 0;

		if(isReset) {
			dom.innerHTML = '';
			//          counterArr[counterIndex] = 0;
		}
		urlr = getUrl();
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urlr + "/print/getBillPrintList",
			data: index,
			async: true,
			dataType: "json",
			success: function(msg) {
//				console.log(msg);
				if(msg.code == 200) {
//					console.log(msg.returnString)
//					console.log(JSON.parse(msg.returnString));
					var datapp = JSON.parse(msg.returnString);
					var count = msg.count
					$("#ppp").html(msg.count);
					aa(datapp)
				} else {
					alert(msg.disp)					
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});

		function aa(datapp) {
			var template = '<li><p><span>单据类型：</span><s>{{title}}</s></p><p><span>单据性质：</span>	<s>{{title1}}</s></p><p>	<span>单据时间：</span><s>{{title2}}</s></p><p>	<span>房屋：</span><s>{{title3}}</s>	</p><p><span>单据金额：</span><s>{{title4}}</s></p><input type="button"  onclick="open_az(teldatapp.{{title5}},{{title6}})" class="sub" value="打印" /></li><div class="kong"></div>	';
//			console.log(datapp)
			var html = '';			
			for(var i = 0; i < datapp.length; i++) {			
				console.log(datapp[i].billtype);
				var billid = 'DI'+datapp[i].billid;
				teldatapp[billid] = billid;	
				console.log(teldatapp);	
				//
				html += exports.renderTemplate(template, {
					title: datapp[i].costname,
					title1: datapp[i].billtypename,
					title2: datapp[i].billdate,
					title3: datapp[i].areaname + datapp[i].buildname + datapp[i].unitname + datapp[i].rommcode,
					title4: datapp[i].payamt,
					title5: 'DI'+datapp[i].billid,
					title6: datapp[i].billtype,
					datapp: datapp[i]
				});
//				teldatapp.{{title5}};
//				console.log(teldatapp.billid);
				//          counterArr[counterIndex]++;
			}
			var child = exports.parseHtml(html);
			dom.appendChild(child);
		};

	};
	
	/**
	 * 绑定监听事件 暂时先用click
	 * @param {String} dom 单个dom,或者selector
	 * @param {Function} callback 回调函数
	 * @param {String} eventName 事件名
	 */
	exports.bindEvent = function(dom, callback, eventName) {
		eventName = eventName || 'click';
		if(typeof dom === 'string') {
			// 选择
			dom = document.querySelectorAll(dom);
		}
		if(!dom) {
			return;
		}
		if(dom.length > 0) {
			for(var i = 0, len = dom.length; i < len; i++) {
				dom[i].addEventListener(eventName, callback);
			}
		} else {
			dom.addEventListener(eventName, callback);
		}
	};
})(window.Common = {});